var express=require('express')
var routers=express. Router()
var files=require('./files')
        //主页路由
        routers.get('/student',function(req,res){
          files.filesall(function(err,data){
                if(err){
                    return res.status(500).send("Server err")
                }
              res.render('slp.html',{
                    clas:[
                        {"Label":"大一","Something":"一班"},
                        {"Label":"大二","Something":"二班"},
                        {"Label":"大三","Something":"三班"},
                        {"Label":"大四","Something":"四班"}
                    ],
                  students:data.students
              })
            })
        })
        //增加学生get路由
        routers.get('/student/new',function(req,res){
            res.render('add.html')
        })
        //增加学生post路由
        routers.post('/student/new',function(req,res){
            files.filesadd(req.body,function(err){
                if(err){
                    return res.status(500).send("Server err")
                }
                res.redirect('/student');
            })
        })
        //修改学生get路由
        routers.get('/student/alter',function(req,res){
            files.filesalter(parseInt(req.query.id),function(err,student){
            if(err){
                return res.status(500).send("Server err")
            }
                res.render('alter.html',{
                    student:student
                })
            })


        })
        //修改学生post路由
        routers.post('/student/alter',function(req,res){
            files.updata(req.body,function(err){
                if(err){
                    return res.status(500).send("Server err")
                }
                res.redirect('/student');
            })
        })
        //删除学生post路由
        routers.get('/student/delete',function(req,res){
            files.filesdelete(req.query.id,function(err){
                if(err){
                   console.log('删除成功')
                }
                res.redirect('/student');
            })

        })



module.exports=routers
